package org.quotidian.jinger_project;

import org.cometd.Message;
import org.mortbay.cometd.AbstractBayeux;
import org.mortbay.cometd.continuation.ContinuationCometdServlet;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.DefaultServlet;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.resource.Resource;
import org.mortbay.resource.ResourceCollection;
import org.mortbay.thread.QueuedThreadPool;

/**
 * Hello world!
 * 
 */
public class App {
	private static int _testHandshakeFailure;

	public static void main(String[] args) throws Exception {

		int port = args.length == 0 ? 8080 : Integer.parseInt(args[0]);

		App app = new App();
		app.run();

	}

	private void run() throws Exception {

		int port = 8080;
		Server server = constructServer(port);

		/*
		 * SslSelectChannelConnector ssl_connector=new
		 * SslSelectChannelConnector(); ssl_connector.setPort(port-80+443);
		 * ssl_connector.setKeystore(base+"/etc/keystore");
		 * ssl_connector.setPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
		 * ssl_connector.setKeyPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
		 * ssl_connector.setTruststore(base+"/etc/keystore");
		 * ssl_connector.setTrustPassword
		 * ("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
		 * server.addConnector(ssl_connector);
		 */

		ContextHandlerCollection contexts = new ContextHandlerCollection();
		server.setHandler(contexts);

		/*
		 * Kept this because it's interesting. Never seen the movedcontext
		 * before. MovedContextHandler moved = new
		 * MovedContextHandler(contexts,"/","/cometd");
		 * moved.setDiscardPathInfo(true);
		 */

		// Create the root context and mount the webapp dir
		/*
		Context rootContext = new Context(contexts, "/jinger", Context.NO_SECURITY | Context.SESSIONS);
		rootContext.setBaseResource(new ResourceCollection(new Resource[] { Resource.newResource("./src/main/webapp/"), }));
		ServletHolder dftServlet = rootContext.addServlet(DefaultServlet.class, "/");
		dftServlet.setInitOrder(2);
		 */
		
		// Create the com context.
		Context redirectContext = new Context(contexts, "/", Context.NO_SECURITY | Context.SESSIONS);
		// context.setBaseResource(new ResourceCollection(new Resource[] {
		// Resource.newResource("./src/main/webapp/"), }));
		ServletHolder redirectServlet = redirectContext.addServlet(RedirectServlet.class, "/");
		redirectServlet.setInitOrder(1);

		// Create the com context.
		/*
		Context context = new Context(contexts, "/cometd", Context.NO_SECURITY | Context.SESSIONS);
		context.setBaseResource(new ResourceCollection(new Resource[] { Resource.newResource("./src/main/webapp/"), }));
		ServletHolder comet = context.addServlet(ContinuationCometdServlet.class, "/cometd/*");
		comet.setInitParameter("filters", "/WEB-INF/filters.json");
		comet.setInitParameter("timeout", "20000");
		comet.setInitParameter("interval", "100");
		comet.setInitParameter("maxInterval", "10000");
		comet.setInitParameter("multiFrameInterval", "5000");
		comet.setInitParameter("logLevel", "1");
		comet.setInitOrder(3);
		*/
		server.start();

		/*
		final AbstractBayeux bayeux = ((ContinuationCometdServlet) comet.getServlet()).getBayeux();
		bayeux.setSecurityPolicy(new AbstractBayeux.DefaultPolicy() {
			public boolean canHandshake(Message message) {
				if (_testHandshakeFailure < 0) {
					_testHandshakeFailure++;
					return false;
				}
				return true;
			}
		});
		*/
	}

	private Server constructServer(int port) {
		// Manually construct context to avoid hassles with webapp classloaders
		// for now.
		Server server = new Server();
		QueuedThreadPool qtp = new QueuedThreadPool();
		qtp.setMinThreads(5);
		qtp.setMaxThreads(200);
		server.setThreadPool(qtp);

		SelectChannelConnector connector = new SelectChannelConnector();
		connector.setPort(port);
		server.addConnector(connector);
		SocketConnector bconnector = new SocketConnector();
		bconnector.setPort(port + 1);
		server.addConnector(bconnector);
		return server;
	}

}
